import csv

from function.data_deal import compar_hr, compar_rr
from function.fun_data_deal import find_elements_with_substrings, group_by_prefix, longest_common_substring, max_hr, \
    stage_divide, df_get, compar_all, get_stage_hr, get_stage_rr, get_stage_signal
from other_function.check import check, ecg_check, singledetail_check, singlework_check, file_check, stage_check, time_check
from function.merger import merger,to_csv
from other_function.align import ecgrr_ppgrr, all_align

import os

from show.boxpolt import box_compare
from show.img_show import hr_img, rpe_img, hrv_img, rr_img, hrv, ppg_ecg_img,hr_boxpolt, rr_boxpolt

deal_path=r'D:\学习&科研\华为手表项目\华为数据\deal'
subject_path = r'D:\学习&科研\华为手表项目\华为数据\subject'
save_dir=r'D:\学习&科研\华为手表项目\华为数据统计和分析'


def compare(save_dir,step):
    file = open('output2-'+str(step)+'s.csv', mode='w', newline='')
    writer = csv.writer(file)
    writer.writerow(['测试编号','测试阶段','生理rpe','心理rpe','测试状态','rr列表形式','hr列表形式'])

    for i in range(1001,1108):
        if i in [1063,1023 ,1049]:
            pass
        else:
            try:
                person=str(i)
                test=person+'-1-1'
                print(test)
                xlrpe = []
                slrpe = []
                hrvlist = []

                ecg_df,rrdata_df,ppg_df,rri_df,singlework_df,singledetail_df=df_get(deal_path, person, test)



                satge_df = stage_divide(ecg_df)
                singledetail_df['timestamp'] = singledetail_df['timestamp'].str[:13]
                rrdata_df['timestamp'] = rrdata_df['timestamp'].astype('int64')
                rri_df['timestamp'] = rri_df['timestamp'].astype('int64')
                ppg_df['PPG_TIME']=ppg_df['PPG_TIME'].astype('int64')
                ecg_df['ecg_timestamp']=ecg_df['ecg_timestamp'].astype('int64')
                for stage in satge_df.values:
                    print('阶段',stage)
                    start_time = stage[3]
                    end_time = stage[4]
                    #获取PPG,获取ecg
                    ppg_stage,ecg_stage=get_stage_signal(ppg_df,ecg_df,start_time,end_time,step)

                    print(ppg_stage,ecg_stage)
                    # 获取心率
                    hr_stage_rrdata,hr_stage_singledetail=get_stage_hr(rrdata_df,singledetail_df,start_time,end_time,step)
                    # 获取rr
                    stage_rrdata2, stage_rri2=get_stage_rr(rrdata_df,rri_df,start_time,end_time,step)
                    print('rr数据',stage_rrdata2)
                    rr_csv=[rr[1] for rr in stage_rrdata2]
                    hr_csv=[hr[1] for hr in hr_stage_rrdata]
                    row=[test,stage[0],stage[5],stage[6],stage[7],rr_csv,hr_csv]
                    writer.writerow(row)
                    # xlrpe.append(int(stage[5]))
                    # slrpe.append(int(stage[6]))
                    # hrvlist.append(hrv(stage_rrdata2, stage_rri2))



                    # ppg_ecg_img(save_dir,test,stage,ppg_stage,ecg_stage,step)
                    # hr_img(save_dir,test,stage,hr_stage_rrdata,hr_stage_singledetail,step)
                    # rr_img(save_dir,test, stage, stage_rrdata2, stage_rri2,step)
                #
                # hrv_img(save_dir,test, hrvlist, slrpe, xlrpe,step)
                # rpe_img(save_dir,test, stage='rep', xlrpe=xlrpe, slrpe=slrpe,step=step)
            #
            except:
                print("发生错误")
    file.close()



compare(save_dir,0)
compare(save_dir,30)
compare(save_dir,60)
compare(save_dir,90)